﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.IO;

namespace ATW.MT.Common
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpFileCollection files = Request.Files;//这里只能用<input type="file" />才能有效果,因为服务器控件是HttpInputFile类型
            string msg = string.Empty;
            string error = string.Empty;
            string fileType = "";
            if (files.Count > 0)
            {
                string fileExt = Path.GetExtension(files[0].FileName);
                if (fileExt.ToLower() == ".mp4" || fileExt.ToLower() == ".mov" || fileExt.ToLower() == ".m4a")
                {
                    fileType = "video";
                }
                else if (fileExt.ToLower() == ".jpg" || fileExt.ToLower() == ".png")
                {
                    fileType = "pic";
                }
                else
                {
                    fileType = "unknow";
                    Response.Write("{\"error\":'wrong type'}");
                    Response.End();
                    return;
                }
                string filePath = Server.MapPath(ConfigurationManager.AppSettings["UploadFilePath"]);
                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + System.IO.Path.GetFileName(files[0].FileName);
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                string fullPath = filePath + fileName;
                files[0].SaveAs(fullPath);
                files[0].SaveAs(filePath + "s_" + fileName);
                msg = " 成功! 文件大小为:" + files[0].ContentLength;
                string res = "{ \"error\":'" + error + "', \"msg\":'" + msg + "',\"fileName\":'" + fileName + "',\"fileType\":'" + fileType + "'}";
                Response.Write(res);
                Response.End();
            }
            else
            {
                string res = "{ \"error\":'error-no-file', \"msg\":'请选择文件',\"fileName\":'',\"fileType\":''}";
                Response.Write(res);
                Response.End();
            }
        }
    }
}